package edu.byu.intex2.data.bo;

import edu.byu.intex2.data.DataException;
import edu.byu.intex2.data.dao.OrderDAO;
import edu.byu.intex2.data.dao.TechnicianDAO;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Order keeps track of a logical order made by a technician.
 * @author Brady
 */
public class Order extends BusinessObject {

    private Date date;

    /** Prices */
    private double totalPrice;
    private double tax;

    /** Technician */
    private String technicianId;
    private Technician technician;

    /** OrderLines */
    private List<OrderLine> orderLines;

    /** Service Facility */
    private String serviceFacilityId;
    private ServiceFacility serviceFacility;

    /** Manufacturer */
    private String manufacturerId;
    private Manufacturer manufacturer;


    /** Creates a new instance of BusinessObject */
    public Order(String id) {
        super(id);
    }//constructor

    /** Convenience method to call the correct DAO to save this business object */
    public void save() throws DataException {
        try {
            OrderDAO.getInstance().save(this);
        } catch (DataException e) {
            e.printStackTrace();
        }
    }

    /**
     * @return the ordersLines
     */
    public List<OrderLine> getOrdersLines() {
        if (ordersLines == null) {
            ordersLines = new ArrayList<OrderLine>();
        }
        return ordersLines;
    }

    /**
     * @return the totalPrice
     */
    public double getTotalPrice() {
        return totalPrice;
    }

    /**
     * @param totalPrice the totalPrice to set
     */
    public void setTotalPrice(double totalPrice) {
        this.totalPrice = totalPrice;
        setDirty();
    }

    /**
     * @return the tax
     */
    public double getTax() {
        return tax;
    }

    /**
     * @param tax the tax to set
     */
    public void setTax(double tax) {
        this.tax = tax;
        setDirty();
    }

    /**
     * @return the date
     */
    public Date getDate() {
        return date;
    }

    /**
     * @param date the date to set
     */
    public void setDate(Date date) {
        this.date = date;
        setDirty();
    }

    /**
     * @param ordersLines the ordersLines to set
     */
    public void setOrderLines(List<OrderLine> orderLines) {
        this.orderLines = orderLines;
        setDirty();
    }

    /**
     * @return ordersLines the ordersLines to get
     */
    public List<OrderLine> getOrderLines() {
        return orderLines;
    }
    /**
     * Add a orderLine
     * @param orderLine to add
     */
    public void addOrderLine(OrderLine orderLine) {
        getOrdersLines().add(orderLine);
        setDirty(); // Set Dirty
    }

    /** Remove a order line
     * @param orderLine  to remove
     */
    public void removeOrderLine(OrderLine orderLine) {
        getOrdersLines().remove(orderLine);
        setDirty();
    }

    /**
     * @return the technicianId
     */
    public String getTechnicianId() {
        return technicianId;
    }

    /**
     * @param technicianId the technicianId to set
     */
    public void setTechnicianId(String technicianId) {
        this.technicianId = technicianId;
        setDirty();
    }

    /**
     * @return the technician from the DAO
     */
    public Technician getTechnician() {
        if (technician == null) {
            try {
                technician = TechnicianDAO.getInstance().read(technicianId);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return technician;
    }

    /**
     * @param technician the technician to set
     */
    public void setTechnician(Technician technician) {
        this.technician = technician;
    }
    
    /**
     * @return the serviceFacilityId
     */
    public String getServiceFacilityId() {
        return serviceFacilityId;
    }

    /**
     * @param serviceFacilityId the serviceFacilityId to set
     */
    public void setServiceFacilityId(String serviceFacilityId) {
        this.serviceFacilityId = serviceFacilityId;
        setDirty();
    }

    /**
     * @return the serviceFacility from the DAO
     */
    public ServiceFacility getServiceFacility() {
        if (serviceFacility == null) {
            try {
                serviceFacility = ServiceFacilityDAO.getInstance().read(serviceFacilityId);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return serviceFacility;
    }

    /**
     * @param serviceFacility the serviceFacility to set
     */
    public void setServiceFacility(ServiceFacility serviceFacility) {
        this.serviceFacility = serviceFacility;
    }    

    /**
     * @return the manufacturerId
     */
    public String getManufacturerId() {
        return manufacturerId;
    }

    /**
     * @param manufacturerId the manufacturerId to set
     */
    public void setManufacturerId(String manufacturerId) {
        this.manufacturerId = manufacturerId;
        setDirty();
    }

    /**
     * @return the manufacturer from the DAO
     */
    public Manufacturer getManufacturer() {
        if (manufacturer == null) {
            try {
                manufacturer = ManufacturerDAO.getInstance().read(manufacturerId);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return manufacturer;
    }

    /**
     * @param manufacturer the manufacturer to set
     */
    public void setManufacturer(Manufacturer manufacturer) {
        this.manufacturer = manufacturer;
    }

}
